package com.ueboot.weishop.api.controller.customer;

import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.SessionUtils;
import com.ueboot.weishop.api.controller.customer.vo.CustomerReceiverReq;
import com.ueboot.weishop.api.controller.customer.vo.CustomerReceiverResp;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.customer.CustomerReceiverEntity;
import com.ueboot.weishop.service.customer.CustomerReceiverService;
import com.ueboot.weishop.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zl  on 2017-11-04 11:39:14.
 * 我的信息接口
 * - 新增收货地址接口
 * - 查询收货地址接口
 * - 修收货地址接口
 * - 删除收货地址接口
 * - 设置默认地址接口
 *
 * @author zl
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/private/customer/receiver")
public class CustomerReceiverController {

	@Autowired
	private CustomerReceiverService service;

	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/lists", method = RequestMethod.GET)
	public Response<List<CustomerReceiverResp>> list() {
		String openId = SessionUtils.getOpenId();
		if (StringUtils.isEmpty(openId)) {
			throw new BusinessException("请使用微信！");
		}
		CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
		if (customerInfoEntity == null) {
			throw new BusinessException("会员信息不存在");
		}
		List<CustomerReceiverEntity> entities = service.findByCustomerId(customerInfoEntity.getId());
		List<CustomerReceiverResp> body = new ArrayList<>();
		for (CustomerReceiverEntity entity : entities) {
			CustomerReceiverResp resp = new CustomerReceiverResp();
			BeanUtils.copyProperties(entity, resp);
			body.add(resp);
		}
		return new Response<>(body);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Response<Void> save(@RequestBody CustomerReceiverReq req) {
		CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
		CustomerReceiverEntity entity = null;
		if (req.getId() == null) {
			entity = new CustomerReceiverEntity();
		} else {
			entity = service.get(req.getId());
		}
		BeanUtils.copyProperties(req, entity);
		entity.setCustomer(customerInfoEntity);
		entity.setDefaultReceiver("是".equals(req.getDefaultReceiver()));
		service.save(entity);
		return new Response<>();
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public Response<Void> delete(@PathVariable Long id) {
		service.delete(id);
		return new Response<>();
	}

	@RequestMapping(value = "/is/default", method = RequestMethod.POST)
	public Response<Void> isDefault(@RequestBody CustomerReceiverReq req) {
		CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
		service.updateUnDefaultByCustomerId(customerInfoEntity.getId(), Boolean.FALSE);

		CustomerReceiverEntity entity = service.get(req.getId());
		entity.setDefaultReceiver(true);
		service.save(entity);
		return new Response<>();
	}


}
